package com.example.k8s.hello.service.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.example.k8s.hello.service.sys.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import com.example.k8s.hello.common.annotation.SysLog;
import com.example.k8s.hello.common.exception.ApiExEnum;
import com.example.k8s.hello.common.utils.StpExtendUtil;
import com.example.k8s.hello.bean.sys.entity.SysMenuEntity;
import com.example.k8s.hello.bean.sys.constant.SysConstant;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 系统菜单
 *
 * @author Mark sunlightcs@gmail.com
 */
@Tag(name = "系统菜单")
@RestController
@RequestMapping("/sys/menu")
@RequiredArgsConstructor
public class SysMenuController{
	private final SysMenuService sysMenuService;

	/**
	 * 导航菜单
	 */
	@Operation(summary = "导航菜单")
	@GetMapping("/nav")
	public HashMap nav(){
		List<SysMenuEntity> menuList = sysMenuService.getUserMenuList(StpExtendUtil.getUserId());
		Set<String> permissions = StpExtendUtil.getPermissions();

		HashMap<String, Object> map = new HashMap<>();
		map.put("menuList", menuList);
		map.put("permissions", permissions);
		return map;
	}
	
	/**
	 * 菜单列表
	 */
	@Operation(summary = "菜单列表")
	@GetMapping("/list")
	@SaCheckPermission("sys:menu:list")
	public List<SysMenuEntity> list(){
		List<SysMenuEntity> menuList = sysMenuService.list();

		//查询完成 对此list直接排序
		Collections.sort(menuList);

		HashMap<Long, SysMenuEntity> menuMap = new HashMap<>(12);
		for (SysMenuEntity s : menuList) {
			menuMap.put(s.getMenuId(), s);
		}
		for (SysMenuEntity s : menuList) {
			SysMenuEntity parent = menuMap.get(s.getParentId());
			if (Objects.nonNull(parent)) {
				s.setParentName(parent.getName());
			}

		}

		return menuList;
	}
	
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@Operation(summary = "选择菜单(添加、修改菜单)")
	@GetMapping("/select")
	@SaCheckPermission("sys:menu:select")
	public List<SysMenuEntity> select(){
		//查询列表数据
		List<SysMenuEntity> menuList = sysMenuService.queryNotButtonList();
		
		//添加顶级菜单
		SysMenuEntity root = new SysMenuEntity();
		root.setMenuId(0L);
		root.setName("一级菜单");
		root.setParentId(-1L);
		root.setOpen(true);
		menuList.add(root);
		
		return menuList;
	}
	
	/**
	 * 菜单详情
	 */
	@Operation(summary = "菜单详情")
	@GetMapping("/info/{menuId}")
	@SaCheckPermission("sys:menu:info")
	public SysMenuEntity info(@PathVariable("menuId") Long menuId){
		SysMenuEntity menu = sysMenuService.getById(menuId);
		return menu;
	}
	
	/**
	 * 保存菜单
	 */
	@Operation(summary = "保存菜单")
	@SysLog("保存菜单")
	@PostMapping("/save")
	@SaCheckPermission("sys:menu:save")
	public void save(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
		
		sysMenuService.save(menu);
	}
	
	/**
	 * 修改
	 */
	@Operation(summary = "修改菜单")
	@SysLog("修改菜单")
	@PostMapping("/update")
	@SaCheckPermission("sys:menu:update")
	public void update(@RequestBody SysMenuEntity menu){
		//数据校验
		verifyForm(menu);
				
		sysMenuService.updateById(menu);
	}
	
	/**
	 * 删除菜单
	 */
	@Operation(summary = "删除菜单")
	@SysLog("删除菜单")
	@PostMapping("/delete/{menuId}")
	@SaCheckPermission("sys:menu:delete")
	public void delete(@PathVariable("menuId") long menuId){
		ApiExEnum.A0011.assertTrue(menuId > 31, "系统菜单，不能删除");

		//判断是否有子菜单或按钮
		List<SysMenuEntity> menuList = sysMenuService.queryListParentId(menuId);
		ApiExEnum.A0011.assertTrue(menuList.size() == 0, "请先删除子菜单或按钮");

		sysMenuService.delete(menuId);
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(SysMenuEntity menu){
		ApiExEnum.A0010.assertTrue(StringUtils.isNoneBlank(menu.getName()), "菜单名称不能为空");

		ApiExEnum.A0010.assertTrue(menu.getParentId() != null, "上级菜单不能为空");

		//菜单
		if(menu.getType() == SysConstant.MenuType.MENU.getValue()){
			ApiExEnum.A0010.assertTrue(StringUtils.isNotBlank(menu.getUrl()), "菜单URL不能为空");
		}
		
		//上级菜单类型
		int parentType = SysConstant.MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			SysMenuEntity parentMenu = sysMenuService.getById(menu.getParentId());
			parentType = parentMenu.getType();
		}
		
		//目录、菜单
		if(menu.getType() == SysConstant.MenuType.CATALOG.getValue() ||
				menu.getType() == SysConstant.MenuType.MENU.getValue()){
			if(parentType != SysConstant.MenuType.CATALOG.getValue()){
				ApiExEnum.A0010.thrown("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getType() == SysConstant.MenuType.BUTTON.getValue()){
			if(parentType != SysConstant.MenuType.MENU.getValue()){
				ApiExEnum.A0010.thrown("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
